﻿using FreeSql.Aop;
using Microsoft.Extensions.Configuration;
using Syx.Webapi.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhongLiang.Core.Helpers;

namespace Syx.Webapi.AppCode {
    public class Constants2 {
        public IConfiguration Config
        {
            get
            {
                //IConfiguration config = new ConfigurationBuilder().AddJsonFile("ZhongLiang.json").AddJsonFile("appsettings.json").Build();
                IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
                return config;
            }
        }

        public string? HisUrl
        {
            get
            {
                var host = Config["Host"];
                if (host.IsNullOrWhiteSpace()) return null;
                else return host;
            }
        }

        public string? SqlConnection
        {
            get
            {
                var host = Config["ConnectionStrings:MsSql"];
                if (host.IsNullOrWhiteSpace()) return null;
                else return host;
            }
        }

        public string? SqlConnectionOracle {
            get {
                var host = Config["ConnectionStrings:Oracle"];
                if (host.IsNullOrWhiteSpace()) return null;
                else return host;
            }
        }

        /// <summary>
        /// 是否测试
        /// </summary>
        public bool IsDebug
        {
            get
            {
                string m = Config["IsDebug"];
                if (!m.IsNullOrWhiteSpace() && m.ToLower() == "1") return true;
                else return false;
            }
        }
        public bool SqlMonitor
        {
            get
            {
                string m = Config["SqlMonitor"];
                if (!m.IsNullOrWhiteSpace() && m == "1") return true;
                else return false;
            }
        }

        public IFreeSql MyDb
        {
            get
            {
                IFreeSql fsql = new FreeSql.FreeSqlBuilder()
                                .UseConnectionString(FreeSql.DataType.SqlServer, SqlConnection)
                                .UseAutoSyncStructure(IsDebug) //自动同步实体结构【开发环境必备】，FreeSql不会扫描程序集，只有CRUD时才会生成表。
                                .UseMonitorCommand(cmd => {
                                    if (Constants.SqlMonitor) {
                                        Console.Write(cmd.CommandText);
                                        NlogHelper._logger.Info(cmd.CommandText);
                                    }
                                })
                                .Build(); //请务必定义成 Singleton 单例模式
                fsql.Aop.CurdAfter += (s, e) =>
                {
                    if (Constants.IsDebug || e.CurdType != CurdType.Select)
                        Console.WriteLine($"{e.CurdType.ToString()} --  表:{e.Table.DbName}; ManagedThreadId:{Thread.CurrentThread.ManagedThreadId};" + $" 时间:{e.ElapsedMilliseconds}ms");
                    if (e.Exception != null)
                    {
                        throw e.Exception;
                    }

                    //if (e.ElapsedMilliseconds > 900)
                    //{
                    //    //记录日志
                    //    //发送短信给负责人
                    //    Console.WriteLine($"ManagedThreadId:{Thread.CurrentThread.ManagedThreadId};" + $" FullName:{e.EntityType.FullName} ElapsedMilliseconds:{e.ElapsedMilliseconds}ms");
                    //}
                };
                return fsql;
            }
        }

        public IFreeSql MyDbOracle {
            get {
                IFreeSql fsql = new FreeSql.FreeSqlBuilder()
                                .UseConnectionString(FreeSql.DataType.Oracle, SqlConnectionOracle)
                                .UseAutoSyncStructure(IsDebug) //自动同步实体结构【开发环境必备】，FreeSql不会扫描程序集，只有CRUD时才会生成表。
                                .UseLazyLoading(true)
                                .UseNoneCommandParameter(true)
                                .UseMonitorCommand(cmd => {
                                    if (Constants.SqlMonitor) {
                                        Console.Write(cmd.CommandText);
                                        NlogHelper._logger.Info(cmd.CommandText);
                                    }
                                })
                                .Build(); //请务必定义成 Singleton 单例模式
                fsql.Aop.CurdAfter += (s, e) => {
                    if (Constants.IsDebug || e.CurdType != CurdType.Select)
                        Console.WriteLine($"{e.CurdType.ToString()} --  表:{e.Table.DbName}; ManagedThreadId:{Thread.CurrentThread.ManagedThreadId};" + $" 时间:{e.ElapsedMilliseconds}ms");
                    if (e.Exception != null) {
                        throw e.Exception;
                    }

                    //if (e.ElapsedMilliseconds > 900)
                    //{
                    //    //记录日志
                    //    //发送短信给负责人
                    //    Console.WriteLine($"ManagedThreadId:{Thread.CurrentThread.ManagedThreadId};" + $" FullName:{e.EntityType.FullName} ElapsedMilliseconds:{e.ElapsedMilliseconds}ms");
                    //}
                };
                return fsql;
            }
        }
    }
}
